package com.meixiang.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.tool.Tool;

/**
 * 实现商品数量变化的按钮
 *
 * @author LZRUI
 */
public class GoodsNumChangeView extends LinearLayout implements View.OnClickListener {

    /**
     * 数量变化监听接口
     */
    public interface OnNumChangeListener {
        void onMinusSuccess(int currentNum, GoodsNumChangeView view);

        void onAddSuccess(int currentNum, GoodsNumChangeView view);
    }

    /**
     * 获取当前数据
     */
    public interface GetCurrentNumListener {
        void getCurrentNum(int currentNum);
    }

    private Context mContext;
    private ImageButton imgAdd;
    private ImageButton imgMinus;
    private TextView tvNum;

    private int mCurrentNum = 1;
    private int mMaxNum = 10;
    private boolean isAutoChangeNum = true; // 是否自动改变数量，默认是true

    private OnNumChangeListener onNumChangeListener;

    public void setGetCurrentNumListener(GetCurrentNumListener getCurrentNumListener) {
        this.getCurrentNumListener = getCurrentNumListener;
    }

    private GetCurrentNumListener getCurrentNumListener;

    public void setOnNumChangeListener(OnNumChangeListener onNumChangeListener) {
        this.onNumChangeListener = onNumChangeListener;
    }

    public GoodsNumChangeView(Context context) {
        this(context, null);
    }

    public GoodsNumChangeView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @SuppressLint("NewApi")
    public GoodsNumChangeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
        initView();
        checkNum();
    }

    private void init() {
        setOrientation(HORIZONTAL);
        setBackgroundResource(R.drawable.shape_goods_num_change_bg);
        LayoutInflater.from(mContext).inflate(R.layout.layout_goods_num_change, this, true);
    }

    private void initView() {
        imgAdd = (ImageButton) findViewById(R.id.imgBtn_add);
        imgMinus = (ImageButton) findViewById(R.id.imgBtn_minus);
        tvNum = (TextView) findViewById(R.id.tv_num);
        imgAdd.setOnClickListener(this);
        imgMinus.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (Tool.isFastDoubleClick()) return;
        if (v.equals(imgAdd)) {
            if (isAutoChangeNum) {
                mCurrentNum++;
            }
            checkNum();
            tvNum.setText(String.valueOf(mCurrentNum));
            if (onNumChangeListener != null) {
                onNumChangeListener.onAddSuccess(mCurrentNum, this);
            }
        } else if (v.equals(imgMinus)) {
            if (isAutoChangeNum) {
                mCurrentNum--;
            }
            checkNum();
            tvNum.setText(String.valueOf(mCurrentNum));
            if (onNumChangeListener != null) {
                onNumChangeListener.onMinusSuccess(mCurrentNum, this);
            }
        }
        if (getCurrentNumListener != null) {
            getCurrentNumListener.getCurrentNum(mCurrentNum);
        }
    }

    private void checkNum() {
        if (mMaxNum == 0) {
            // 当最大值是0的时候，不能加也不能减
            imgMinus.setEnabled(false);
            imgMinus.setImageResource(R.mipmap.ic_minus_unable);
            imgAdd.setEnabled(false);
            imgAdd.setImageResource(R.mipmap.ic_add_unable);
            return;
        }
        if (mCurrentNum <= 1) {
            // 当数量为小于等于1时，减号按钮不能点击
            imgMinus.setEnabled(false);
            imgMinus.setImageResource(R.mipmap.ic_minus_unable);
        } else {
            imgMinus.setEnabled(true);
            imgMinus.setImageResource(R.mipmap.ic_minus_enable);
        }
        if (mCurrentNum >= mMaxNum) {
            // 当数量到达最大值时，加号按钮不能点击
            imgAdd.setEnabled(false);
            imgAdd.setImageResource(R.mipmap.ic_add_unable);
        } else {
            imgAdd.setEnabled(true);
            imgAdd.setImageResource(R.mipmap.ic_add_enable);
        }
    }

    /**
     * 初始化数量
     *
     * @param num
     */
    public void initNum(int num) {
        this.mCurrentNum = num;
        tvNum.setText(String.valueOf(mCurrentNum));
        checkNum();
    }

    /**
     * 设置最大数量值
     *
     * @param maxNum
     */
    public void setMaxNum(int maxNum) {
        this.mMaxNum = maxNum;
        checkNum();
    }

    /**
     * 设置是否自动改变数量
     *
     * @param auto
     */
    public void setAutoChangeNum(boolean auto) {
        this.isAutoChangeNum = auto;
    }
}
